const usersModel = require('../dao/models/usersModel')
// 令牌引入
const jwt = require('jsonwebtoken')

module.exports.addUsers = async (params) => {
    const result = await usersModel.create(params)
    console.log(result);
    if (result) {
        return {
            code : 1,
            message : '用户数据新增成功'
        }
    }
    return {
        code: 0,
        message :'电影数据新增失败'
    }
}

// 获取电影数据
module.exports.getUsers = async () => {
    const data = await usersModel.find()
    return {
        code:1,
        message :'电影数据获取成功',
        data
    }
}

module.exports.deleteUsers = async (params) => {
    const data = await usersModel.deleteOne(params)
    if (data.deletedCount) {
        return{
            code:1,
            message :'类型删除成功'
        }
    }
    return {
        code :0,
        message:'电影书库删除失败'
    }
}


module.exports.login = async (params) => {
    console.log(params);
    const data = await usersModel.find(params)
    if (data.length ===1) {
     const token =   jwt.sign(
            {user:data[0]} ,//保存的任意用户信息
            'hello',
            {expiresIn: '10h'}

        );
        return{
            code:1,
            message :'登录成功',
            token: 'Bearer ' + token
        }
    }
    return {
        code :0,
        message:'登录失败'
    }
}